// pages/detail/detail.js
import { shareOptions } from "../../utils/access";
import request from "../../utils/request";
const api = require("../../config/api");
const wxParse = require("../../utils/wxParse/wxParse.js");

Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		lastPayTime: "",
		timer: "",
		overTime: "",
		goodsMoney: "",
		goodsPrice: "",
		orderNo: "",
		payEndTimeStamp: "",
		orderPrice: "",
		payFlag: true,
		payBtnFlag: true,
		payType: 20,
		payEndTimeStamps: "",
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.setData({
			orderNo: options.orderNo,
		});
		this.getGoodsInfo(options.orderNo);
	},
	async getGoodsInfo(orderNo) {
		const res = await request.get(
			api.idleOrderDetail,
			{ orderNo },
			{ loading: false }
		);
		this.setData({
			payEndTimeStamp: res.data.payEndTime,
			payEndTimeStamps: res.data.payEndTimeStamp,
			orderPrice: res.data.orderPayPrice,
		});
		console.log(this.data.payEndTimeStamp);
		this.countDown();
	},
	countDown() {
		this.data.timer = setInterval(this.setIntervalTime, 1000);
	},
	setIntervalTime() {
		let _this = this;
		const end = this.data.payEndTimeStamps;
		const now = Date.parse(new Date());
		let msec = end - now;
		let hr = parseInt((msec / 1000 / 60 / 60) % 24);
		let min = parseInt((msec / 1000 / 60) % 60);
		let sec = parseInt((msec / 1000) % 60);
		if (msec > 1) {
			hr = hr > 9 ? hr : "0" + hr;
			min = min > 9 ? min : "0" + min;
			sec = sec > 9 ? sec : "0" + sec;
			_this.setData({
				lastPayTime: `剩余支付时间: ${hr}:${min}:${sec}`,
				payFlag: true,
			});
		} else {
			_this.setData({
				lastPayTime: "未在规定时间内支付，订单已失效！",
				payFlag: false,
			});
		}
	},
	async handleClick() {
		if (!this.data.payFlag) {
			wx.reLaunch({
				url: "/pages/idleHome/idleHome",
			});
		}
		let { orderNo, orderPrice, payType } = this.data;
		const res = await request.post(
			api.idleOrderPay,
			{ orderNo, orderPrice, payType },
			{ loading: false }
		);

		if (res.httpStatus == 200) {
			this.doWxPay(res.data);
			wx.showLoading({
				title: "支付中…",
				icon: "loading",
				duration: 10000,
				mask: true,
			});
		}
	},
	//微信支付
	doWxPay(param) {
		wx.hideLoading();
		let { orderNo } = this.data;
		wx.requestPayment({
			timeStamp: param.timeStamp,
			nonceStr: param.nonceStr || '',
			package: param.package,
			signType: "MD5",
			paySign: param.sign,
			success: function (event) {
				wx.showToast({
					title: "支付成功",
					icon: "success",
					duration: 2000,
				});
				wx.reLaunch({
					url: `/pages/idleCheSucceed/idleCheSucceed?orderNo=` + orderNo,
				});
			},
			fail: function (error) {
				console.log('支付失败：', error)
				wx.showToast({
					title: "支付失败!",
					icon: "none",
					duration: 2000,
				});
				return;
			},
			complete: function () {
				console.log("pay complete");
			},
		});
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {},

	// 用户点击右上角分享
	onShareAppMessage(e) {
		return shareOptions('default', e)
	},
});
